# -*- coding: utf-8 -*-
import wsgiref.handlers

from google.appengine.ext import webapp
from google.appengine.ext.webapp import template
from BeautifulSoup import BeautifulSoup
from appengine_utilities import sessions
import xmw
from xmw.db import ds
import simplejson
import logging
from xmw.db import user_mgr

class MainPage(xmw.RequestHandler):
    def get(self):
        self.sess = sessions.Session()
        self.cookie_sess = sessions.Session(writer="cookie")    
        session_user =None
        if self.sess.has_key("user"):
            session_user=self.sess["user"]
        
        
        (sub_req_id,e) = self.get_key_from_GET('sub_req_id')
        if sub_req_id is not None:
            sub_req_id  =sub_req_id.strip().decode('utf-8')
            #seq =self.xmw_decoding(url)
            logging.log( logging.INFO, "sub_req_id:%s"% sub_req_id)  
            
        q_sub_req = ds.Sub_Req.all()
        q_sub_req.filter('sub_req_id =',sub_req_id)
        #q_sub_req.filter('status !=',ds.Sub_Req_Status.DELETE)
        sub_req = q_sub_req.get()
        title= sub_req.title
        url = sub_req.url
        option=sub_req.option
        sub_req_id=sub_req.sub_req_id
        #if sub_req is not None: 
        template_values ={"session_user":session_user,
                          "title":title,
                          "url":url,
                          "sub_req_id":sub_req_id,
                          }
        if option == ds.Sub_Req_Option.chrAuthor_Only:
            template_values['checked_chrAuthor']='checked'
        else:
            template_values['checked_all']= 'checked'
            
        self.render_template(template_values, "../html/j/subscribe/get_sub_option.html")
        return
    
        (sub_ack,e) = self.get_key_from_GET('sub_ack')
        if sub_ack is  None:
            #1.user should not sub the same url more than once
            #2.if the url already subed by other user,should fill the req's thread
            #info directly.
            
            #first check if the user has already sub the same thread
            if session_user is not None:
                sub_req=ds.Sub_Req().all()
                sub_req.filter('url =',url)
                sub_req.filter('user_id =',session_user.user_id)
                sub_req.filter('status !=', ds.Sub_Req_Status.DELETE)
                sub=sub_req.get()
                if sub is not None:
                    template_values["already_sub"]=True
                    
            self.render_template(template_values, "../html/j/subscribe/sub.html")
            return
        
        if sub_ack is not None:
            #sub_ack  =seq.strip().decode('utf-8')
            #seq =self.xmw_decoding(url)
            logging.log( logging.INFO, "sub_ack:%s"% sub_ack) 
            
            (watch,e) = self.get_key_from_GET('watch')
            #0 or 1
            watch=watch.strip().decode('utf-8')
            watch=int(watch)
            
            sub_req=ds.Sub_Req()
            sub_req.user_id =session_user.user_id
            sub_req.title   =title.decode('utf-8')
            sub_req.url     =url
            sub_req.created =xmw.get_cn_now_time()
            sub_req.option=watch
            #second,check if the thread already in db by url.
            #if,fill the empty sub_req field by info from thread in db
            
            thread_q=xmw.Thread.all()
            thread_q.filter('lst_page_url  =',url)
            thread=thread_q.get()
            if thread is not None:
                sub_req.thread_id=thread.thread_id
                sub_req.status = ds.Sub_Req_Status.IN_TRACE
                sub_req.title=thread.title
            sub_req.save()
            self.render_template(template_values, "../html/j/subscribe/sub_ack.html")
            return
        #self.response.out
        
        return
    
def main():
    application = webapp.WSGIApplication(
                                       [('/j/subscribe/get_sub_option', MainPage),],
                                       debug=True)
    wsgiref.handlers.CGIHandler().run(application)

if __name__ == "__main__":
  main()